import 'package:flutter/material.dart';
import 'package:flutter_shop/pages/login.dart';
import 'package:flutter_shop/pages/tabs.dart';
import 'package:flutter_shop/pages/detail.dart';
import 'package:flutter_shop/pages/testDemo.dart';
import 'package:flutter_shop/provider/status.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(ChangeNotifierProvider(
    create: (context) => StatusProvider(),
    child: const MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '小测试',
      theme: ThemeData(
        primarySwatch: Colors.pink,
      ),
      // 定义命名路由
      routes: {
        // "/" 相当于设置了首页，就不用再设置home
        "/": (context) => const HomePage(),
        "login": (context) => const Login(),
        "detail": (context) => const GoodsDetail()
      },
      // 路由拦截
      onGenerateRoute: (RouteSettings s) {
        switch (s.name) {
          case "test":
            return MaterialPageRoute(builder: (context) {
              return TestDemo();
            },settings: s);
            // break;
          default:
        }
      },
      // 设置默认首页的路由
      // initialRoute: "login",
      // home: const HomePage(),
      // home: const ProviderDemo(),
      // 不展示debugger标签
      debugShowCheckedModeBanner: false,
    );
  }
}

//设置在首页
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Tabs(),
    );
  }
}

// providerDemo
class ProviderDemo extends StatefulWidget {
  const ProviderDemo({Key? key}) : super(key: key);

  @override
  State<ProviderDemo> createState() => _ProviderDemoState();
}

class _ProviderDemoState extends State<ProviderDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("全局状态管理"),
      ),
      body: Text(Provider.of<StatusProvider>(context).userName),
      floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.change_circle),
          onPressed: () {
            context.read<StatusProvider>().changName();
          }),
    );
  }
}
